package vashion.azeroth.back.controller.item;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.back.controller.base.BaseController;
import vashion.azeroth.contant.ForestErrorEnum;
import vashion.azeroth.core.item.pojo.ForestDO;
import vashion.azeroth.core.item.pojo.ForestTreeAndBrandDO;
import vashion.azeroth.core.item.pojo.ForestTreeDO;
import vashion.azeroth.core.manager.forest.BaseForestManager;
import vashion.azeroth.core.result.ApiResult;

@Controller
@RequestMapping("/back/forest")
public class BackForestController<T> extends BaseController {

    private final static Logger log = LoggerFactory.getLogger(BackForestController.class);

    @Autowired
    private BaseForestManager baseForestManager;

    @RequestMapping("publishForest.htm")
    @ResponseBody
    public ApiResult<ForestDO> publishForest(@ModelAttribute("forestDO") ForestDO forestDO,
            								 @RequestParam(value = "brandIds", required = false) String brandIds,
                                             HttpServletRequest request) {

        ApiResult<Boolean> publishResult = baseForestManager.publishForest(forestDO, brandIds, getLoginUserId(request));
        
        if (publishResult.isSuccess() && publishResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(publishResult.getErrorCode(), publishResult.getErrorMsg());
        }
    }

    @RequestMapping("updateForest.htm")
    @ResponseBody
    public ApiResult<ForestDO> updateForest(@ModelAttribute("forestDO") ForestDO forestDO,
			 								@RequestParam(value = "brandIds", required = false) String brandIds,
                                            HttpServletRequest request) {
        ApiResult<Boolean> updateResult = baseForestManager.updateForest(forestDO, brandIds, getLoginUserId(request));
        if (updateResult.isSuccess() && updateResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(updateResult.getErrorCode(), updateResult.getErrorMsg());
        }
    }

    @RequestMapping("freezeForest.htm")
    @ResponseBody
    public ApiResult<Boolean> freezeForest(@RequestParam(value = "forestId", required = true) long forestId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> deleteResult = baseForestManager.deleteForest(forestId, getLoginUserId(request));
    	
    	if (deleteResult.isSuccess() && deleteResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(deleteResult.getErrorCode(), deleteResult.getErrorMsg());
        }
    }

    @RequestMapping("resumeForest.htm")
    @ResponseBody
    public ApiResult<Boolean> resumeForest(@RequestParam(value = "forestId", required = true) long forestId,
    		                              HttpServletRequest request) {
    	ApiResult<Boolean> resumeResult = baseForestManager.resumeForest(forestId, getLoginUserId(request));
    	
    	if (resumeResult.isSuccess() && resumeResult.getData()) {
            return ApiResult.newSuccessResult();
        } else {
            return ApiResult.newErrorResult(resumeResult.getErrorCode(), resumeResult.getErrorMsg());
        }
    }

    @RequestMapping("getForestDetailWithBrandList.htm")
    @ResponseBody
    public ApiResult<ForestTreeAndBrandDO> getForestDetailWithBrandList(long forestId) {
        if (forestId <= 0) {
            return ApiResult.newErrorResult(ForestErrorEnum.ARG_ERROR.getErrorCode(), ForestErrorEnum.ARG_ERROR.getErrorMessage());
        }
        return baseForestManager.getDetailWithBrandList(forestId);
    }


    /**
     * 获取完整类目树
     *
     * @return
     */
    @RequestMapping("getForestTreeByTitle.htm")
    @ResponseBody
    public ApiResult<List<ForestTreeDO>> getForestTreeByTitle(@ModelAttribute("forestDO") ForestDO forestDO) {

    	forestDO.setFirstRow((forestDO.getPageNum() - 1) * forestDO.getPageSize());
    	ApiResult<List<ForestTreeDO>> result = baseForestManager.getForestTreeByTitle(forestDO);
        if (!result.isSuccess()) {
            return ApiResult.newErrorResult(ForestErrorEnum.SYSTEM_ERROR.getErrorCode(), ForestErrorEnum.SYSTEM_ERROR.getErrorMessage());
        }
        return result;
    }

    /**
     * 查询一级或者二级类目树
     *
     * @return
     */
    @RequestMapping("getParentsOrChildrenForestList.htm")
    @ResponseBody
    public ApiResult<List<ForestDO>> getParentsOrChildrenForestList(@ModelAttribute("forestDO") ForestDO forestDO) {

        forestDO.setFirstRow((forestDO.getPageNum() - 1) * forestDO.getPageSize());
        ApiResult<List<ForestDO>> forestListResult = baseForestManager.getParentsOrChildrenForestList(forestDO);
        forestListResult.setTotalCount(baseForestManager.findCnt(forestDO));
        return forestListResult;
    }

}
